import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/base/BaseController.dart';
import 'package:xin_nuo_pda/http/api/eqp_api.dart';

import 'PeConfirmState.dart';

class PeConfirmController extends BaseController  with SingleGetTickerProviderMixin{
  final PeConfirmState state = PeConfirmState();

  @override
  void onInit() {
    super.onInit();
    state.sheetId = Get.arguments;
    state.tabController = TabController(length: 2, vsync: this);
    state.tabController!.addListener(() {
      state.idx =state.tabController!.index;
      update();
    });

  }

  @override
  void onReady() {
    super.onReady();
    getRepairInfo();
  }

  repairHandler(String type) async{
    if(state.userController.text.isEmpty){
      Get.snackbar('提示', '请输入操作员');
      return;
    }
    if(state.contentController.text.isEmpty){
      Get.snackbar('提示', '请输入维修说明');
      return;
    }
    Map<String, dynamic> params = {
      'SHEETID': state.sheetId,
      'REPAIROWNER': state.userController.text,
      'REPAIRCOMMENT': state.contentController.text,
      'STARTREPAIRTIME': state.repairInfoEntity?.TROUBLETIME,
      'ENDREPAIRTIME': state.selectDate.year.toString() +
          "-" +
          (state.selectDate.month < 10
              ? '0' + state.selectDate.month.toString()
              : state.selectDate.month.toString()) +
          '-' +
          (state.selectDate.day < 10
              ? '0' + state.selectDate.day.toString()
              : state.selectDate.day.toString()) + ' ' + state.selectDate.hour.toString() + ":" + state.selectDate.minute.toString() + ":00",
      'EVENTID': type,
    };
    var result = await EqpApi.repairHandler(canelToken, params: params);
    if(result.ok){
      Fluttertoast.showToast(msg: '处理成功');
      Get.back();
    }
  }

  getRepairInfo() async {
    Map<String, dynamic> params = {'SHEETID': state.sheetId};
    var result = await EqpApi.getRepairInfo(canelToken, params: params);
    if (result.ok) {
      state.repairInfoEntity = result.data;
      update();
    }
  }

  void updateSelectDate(DateTime dateTime) {
    state.selectDate =dateTime;
    update([state.dateChooseId]);
  }
}
